package patterns.behavioral.strategy;

import java.util.ArrayList;
import java.util.List;

/**
 * 策略模式示例
 * 定义一系列的算法，把它们一个个封装起来，并且使它们可相互替换
 */
public class StrategyPattern {

    public static void main(String[] args) {
        // 支付示例
        System.out.println("===== 支付策略示例 =====");
        ShoppingCart cart = new ShoppingCart();
        
        cart.addItem(new Item("书籍", 100));
        cart.addItem(new Item("电子产品", 200));
        cart.addItem(new Item("食品", 50));
        
        // 使用支付宝支付
        cart.setPaymentStrategy(new AlipayStrategy("user@example.com"));
        cart.checkout();
        
        // 使用微信支付
        cart.setPaymentStrategy(new WeChatPayStrategy("wx123456"));
        cart.checkout();
        
        // 使用信用卡支付
        cart.setPaymentStrategy(new CreditCardStrategy("张三", "1234-5678-9012-3456", "123", "12/25"));
        cart.checkout();
        
        // 排序示例
        System.out.println("\n===== 排序策略示例 =====");
        List<Person> people = new ArrayList<>();
        people.add(new Person("张三", 25));
        people.add(new Person("李四", 30));
        people.add(new Person("王五", 20));
        
        Sorter<Person> sorter = new Sorter<>();
        
        // 按姓名排序
        System.out.println("按姓名排序:");
        sorter.setStrategy(new NameSortStrategy());
        sorter.sort(people);
        printPeople(people);
        
        // 按年龄排序
        System.out.println("按年龄排序:");
        sorter.setStrategy(new AgeSortStrategy());
        sorter.sort(people);
        printPeople(people);
    }
    
    private static void printPeople(List<Person> people) {
        for (Person person : people) {
            System.out.println(person);
        }
    }
}

/**
 * 支付策略示例
 */
// 策略接口
interface PaymentStrategy {
    void pay(int amount);
}

// 具体策略：支付宝支付
class AlipayStrategy implements PaymentStrategy {
    private String email;
    
    public AlipayStrategy(String email) {
        this.email = email;
    }
    
    @Override
    public void pay(int amount) {
        System.out.println(amount + "元已通过支付宝支付，账号: " + email);
    }
}

// 具体策略：微信支付
class WeChatPayStrategy implements PaymentStrategy {
    private String id;
    
    public WeChatPayStrategy(String id) {
        this.id = id;
    }
    
    @Override
    public void pay(int amount) {
        System.out.println(amount + "元已通过微信支付，账号: " + id);
    }
}

// 具体策略：信用卡支付
class CreditCardStrategy implements PaymentStrategy {
    private String name;
    private String cardNumber;
    private String cvv;
    private String expiryDate;
    
    public CreditCardStrategy(String name, String cardNumber, String cvv, String expiryDate) {
        this.name = name;
        this.cardNumber = cardNumber;
        this.cvv = cvv;
        this.expiryDate = expiryDate;
    }
    
    @Override
    public void pay(int amount) {
        System.out.println(amount + "元已通过信用卡支付，卡号: " + cardNumber);
    }
}

// 上下文：购物车
class ShoppingCart {
    private List<Item> items;
    private PaymentStrategy paymentStrategy;
    
    public ShoppingCart() {
        this.items = new ArrayList<>();
    }
    
    public void addItem(Item item) {
        items.add(item);
    }
    
    public void removeItem(Item item) {
        items.remove(item);
    }
    
    public int calculateTotal() {
        int sum = 0;
        for (Item item : items) {
            sum += item.getPrice();
        }
        return sum;
    }
    
    public void setPaymentStrategy(PaymentStrategy paymentStrategy) {
        this.paymentStrategy = paymentStrategy;
    }
    
    public void checkout() {
        int amount = calculateTotal();
        paymentStrategy.pay(amount);
    }
}

// 商品类
class Item {
    private String name;
    private int price;
    
    public Item(String name, int price) {
        this.name = name;
        this.price = price;
    }
    
    public String getName() {
        return name;
    }
    
    public int getPrice() {
        return price;
    }
}

/**
 * 排序策略示例
 */
// 策略接口
interface SortStrategy<T> {
    void sort(List<T> items);
}

// 具体策略：按姓名排序
class NameSortStrategy implements SortStrategy<Person> {
    @Override
    public void sort(List<Person> items) {
        items.sort((p1, p2) -> p1.getName().compareTo(p2.getName()));
    }
}

// 具体策略：按年龄排序
class AgeSortStrategy implements SortStrategy<Person> {
    @Override
    public void sort(List<Person> items) {
        items.sort((p1, p2) -> p1.getAge() - p2.getAge());
    }
}

// 上下文：排序器
class Sorter<T> {
    private SortStrategy<T> strategy;
    
    public void setStrategy(SortStrategy<T> strategy) {
        this.strategy = strategy;
    }
    
    public void sort(List<T> items) {
        strategy.sort(items);
    }
}

// 人员类
class Person {
    private String name;
    private int age;
    
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
    
    public String getName() {
        return name;
    }
    
    public int getAge() {
        return age;
    }
    
    @Override
    public String toString() {
        return "Person{name='" + name + "', age=" + age + "}";
    }
} 